// widgets/UserWidget/UserHeadWidget/UserHeadWidget.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    user: {
      type: Object,
    },
    gradeList: {
      type: Array,
    },
    userColGoodsSum: {
      type: String,
    },
    totalSum: {
      type: String,
    },
    intrgral: {
      type: String,
    },
    userSonSum: {
      type: Number,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
    CustomBar: app.globalData.CustomBar,
    focus: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击用户头像，获取微信头像信息
    changeUserInfo(){
      this.triggerEvent('changeUserInfo')
    },
    // 跳转设置页面
    setUser(){
      wx.navigateTo({
        url: '/pages/Users/UserInfo/UserInfo',
      })
    },
    // 页面跳转
    goNext(e){
      let type = e.target.dataset.type||e.currentTarget.dataset.type;
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      // return
      if(item){
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}?item=${JSON.stringify(item)}`,
          })
        }
      }else{
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}`,
          })
        }
        
      }
    },
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail 
      console.log(avatarUrl,'avatarUrl')
      this.triggerEvent('changeUserInfoAvatarUrl',avatarUrl)
    },
    nickNameIptBlur(){
      this.setData({
        focus: true,
      })
      
    },
    inputWord(e){
      console.log(e)
      wx.createSelectorQuery().in(this) // 注意这里要加上 in(this)
      .select(".userNicknamIpt")
      .fields({
          properties: ["value"],
      })
      .exec((res) => {
        var nickName = ''
        if(res.length > 0){
          nickName = res[0].value
        }
        console.log('昵称', res)
        if(nickName != ''){
          this.triggerEvent('changeUserInfoNickname',nickName)
        }
      })
    },
    // 展示等级规则说明
    showGradeDesc(e){
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      console.log(item,'item');
      if(!item.upgradeDescription){
        return;
      }
      wx.showModal({
        showCancel: false,
        title: '温馨提示',
        content: item.upgradeDescription,
      })
    },
  }
})
